package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102800774735";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCI4BkA4D8Y5hjWoX3YdZXC3a3ujH3X9us3w8xLzQM7w/8I5GKQk1SsnWQHeJWIORH5GW5B0nvD8q05Ps5VQjV8k5UTLAurP9MIXHMdA5R/HIA9+x+oXsTBHG3WuBOe9iSDOKRGjEyM8cxAmNo8OgVZjO3HRibW7F5HhOzD5Pz5sR/oVUK6eWG3uJTEm87Gw6uIHqU8iSsPFFJmg+Ea51bBwRf2ko1zw2+I/xK9iwzmOo6Bn+UgsC8cLQPIdp/TlU5r3ssAKr/TlhY4sX+HOUCfTyWBz0rB7102z5+vCXuigtkpsdjDdAL2fkiue8UTZmV/OVznYemqDYD4PYOC3+pnAgMBAAECggEAXgK1ibF8BQj9ZGWU/xh8HVSPAzqBsNTKaAnWhvWv09vhjmT3Ad//l3Oe4bbD1cWE/7ek0BbWpRZIPfpSU6LfA8qF6J4nAZ8bvjFpazMrSkb0Gh4id01oDE8PbhwOde8P0wUTZ9UzOtdU98VEl3suc3xlUKpxQMsmUi8NQ9tCmeg5j62lXmdbazyWXgGiy3vSzGblcoTa33f/e433shRmGkO+scsigHBEAdCxZwO2Er86WhMY3E/mwFN38jvkneFk9JQXREZerOxNq3dPlWnrYe7U+ohgJnJtkT7erlGPBPloR9DbXcSlNU74Ea2qVB9uQZcbtemBltiKoUblL+JhyQKBgQDIJWhv4KxScDghVRdZ7xL0J4dLIQcz3M3rnswjBGJOqLX+xNxaXgnrbc+i5nr3nG2I0YWPz6bhIcNUfjEVCCdGQIOCLOp7FwDhZ+Jt33RZiCfy2Ojkky3IyACc5t2qJMHBTXjm4lC8kDVt6iHiX1KGivjDXt99nVMY3uO807RkGwKBgQCvEpRYzV1UPU3zT1mvC43xYVf61/4fJEpyBUvzY3kmYmMOvmAWWAz32My+rx1uPWmOspdhDX2DATgea6x7b0eAcvbk4wePrnebRFtT2cwHrGtatoTY/fkvFoXlY5/+1pqLKYXQB/V4VXXw3qCxFVqIZORRGDx+mXmxx0CXIPZ/pQKBgFS82cm8QtkoX+tk3l0aRlkOv05Eror7qkde1SjiO0j4Tw2NhYnRFLwTovxiPp/FH+70XmrzzMC2jb+hKeCwsgghWkxr/d6db6b/V7OTDuy1I8zCn1/fHFxMqU3ExQa7RzkzNsPx/6LGH4jovu3b09HY1tfIvDCvwjXecGdK7q4PAoGAImIMvm/+v1eW4o0X+PfU9a3/gGVBJE6VTFVb+D32AiLaf3/GET1Ttq1ecKilqAvpmjva3RWwK7VmlxrgchGFwmVRtdwclQxXsye2o/4nQu6K2vLznkYq1SOgQk/RkVQGYARUommdvPdTxW2Ds6OU9yqRaJfQ8bAkpVSTi9rObn0CgYBhiSVYtchLQeqDU1CrmrwdC/w6FQy/C0L9unYuOxwBSsqfpTJ459r7kpvXSrZk4BNPVKGNiojADMDLjlNe2PkG0tGC7evNd6Lm+iuqg14oZ8gEdwPGm5O/maubUe9yfvTcMEePNKn5IEZ3RCSHyPpUkcTjB/qAAa85jz8R0QcYJA==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuMIart3m0oouhckFZFm8KcvfcAZV+WAMA3rma9meebvWROYyNtRWXV3GSmTmwfsTzOoRuZaEAzDXROoU8tCpeuqOMttBdfSj7Lj96l/GF+Ku2P3i5I66aifdvKJJLoVEbe7n0nt5GtF9do1d5WP1h/gPjBr6j0lSmOcsAwPIj+L77MOA8+tr9T2S8KSSNaIy72EXh4HkOAyX00iaL2D3XtnvSzTtnNsnNQztL51ddIpVC/L6UPmGIUVRXLOleIqAyXepDBXEOY/g4oM2ctIykRmJUvvLeKa/FdGvxlTt4zPFWvcfVFp2aU+PffzJbbiXEtl/zJ8aD09S7l3KeEi84QIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public static String notify_url = "http://47.115.37.249:8080/payed/notify";


    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //收单时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
